package com.aishua.fire.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.constant.DictConstants;
import com.aishua.fire.common.constant.SoftdelConstants;
import com.aishua.fire.entity.perm.PermInfo;
import com.aishua.fire.jpa.OneLevelMenuDao;
import com.aishua.fire.jpa.PermDao;
import com.aishua.fire.jpa.PermPermteamDao;
import com.aishua.fire.jpa.TwoLevelMenuDao;
import com.aishua.fire.jpa.entity.OneLevelMenu;
import com.aishua.fire.jpa.entity.Perm;
import com.aishua.fire.jpa.entity.PermPermteam;
import com.aishua.fire.jpa.entity.PermPermteamId;
import com.aishua.fire.jpa.entity.TwoLevelMenu;
import com.aishua.fire.mapper.PermMapper;
import com.aishua.fire.mapper.dto.PermDto;
import com.aishua.fire.service.PermService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class PermServiceImpl implements PermService{

	@Autowired
	private PermDao permdao;
	@Autowired
	private PermPermteamDao permteampermdao;
	@Autowired
	private  PermMapper permMapper;
	/**
	 * 添加权限信息
	 * @param permInfo
	 * 
	 */
	@Override
	public void addPermInfo(PermInfo permInfo) {
		
		Perm perm = new Perm(permInfo.getPermName(),permInfo.getPermDesc(),permInfo.getRequestMethod(),new Date(),SoftdelConstants.DEL_N);
		
		permdao.save(perm);
		
	}

	/**
	 * 更新权限信息
	 * @param permteaminfo
	 * 
	 */
	@Override
	public void updatePermInfo(PermInfo perminfo,String permId) {
		
		Perm perm = permdao.findOne(Long.parseLong(permId));
		perm.setPermId(Long.parseLong(permId));
		perm.setPermName(perminfo.getPermName());
		perm.setPermDesc(perminfo.getPermDesc());
		perm.setRequestMethod(perminfo.getRequestMethod());
		perm.setCreateDate(new Date());
		perm.setStatus(SoftdelConstants.DEL_N);
		permdao.saveAndFlush(perm);
	}
	
	/**
	 * 删除对应限信息
	 * @param permTeamId
	 * 
	 */
	@Override
   public void deletePermInfo(String permId) {
		
	    
		
		Long permid = Long.parseLong(permId);
		
		Perm perm = permdao.findOne(permid);
		
		perm.setPermId(permid);
		perm.setStatus(SoftdelConstants.DEL_Y);
		permdao.saveAndFlush(perm);
		
		
	}
	
	/**
	 * 查询三级菜单
	 */
	public List<PermDto> getPermThreeLevel(String controllerValue,String permteamId){
		long permteamid = Long.parseLong(permteamId);
		String controllerVal = "com.aishua.fire.controller." + controllerValue;
		return permMapper.getPermThreeLevel(controllerVal, permteamid);
	}
	
	/**
	 * 分页列表查询
	 */
	@Override
	public Page<Perm> searchList(Integer page, Integer size,final String permName){
		Pageable pageable = (Pageable) new PageRequest(page, size, new Sort("permName"));
		return permdao.findAll(new Specification<Perm>() {
            
			@Override
            public Predicate toPredicate(Root<Perm> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
               
                Path<String> permNamePath = root.get("permName");
                /**
                 * 连接查询条件, 不定参数，可以连接0..N个查询条件
                 */
                
                query.where(cb.equal(permNamePath, permName)); //这里可以设置任意条查询条件
                return null;
            }
        },pageable);
	}
	
	/**
	 * 权限组通过权限id查询权限组信息
	 */
	@Override
	public  List<Perm> getPermListByPermteamId(final String permteamId){
		
     final List<PermPermteam> PermPermteamList = permteampermdao.findAll(new Specification<PermPermteam>() {
            
			@Override
            public Predicate toPredicate(Root<PermPermteam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<PermPermteamId> userNamePath = root.get("permpermteamId");
                //连接查询条件, 不定参数，可以连接0..N个查询条件
                query.where(cb.equal(userNamePath.get("permteamId").as(Long.class), permteamId)); //这里可以设置任意条查询条件
                return null;
            }
        });
		
		final List<Long> longs = new ArrayList<>();
		for(int i = 0;i<PermPermteamList.size();i++) {
			longs.add(PermPermteamList.get(i).getPermpermteamId().getPermId());
		}
		
		log.info("PermPermteamList的值是:{}",PermPermteamList);
		return permdao.findAll(new Specification<Perm>() {
		    
			@Override
		    public Predicate toPredicate(Root<Perm> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(root.get("permId").in(longs));
		        return null;
		    }
		});

		
	}

	
	@Autowired
	private OneLevelMenuDao oneLevelMenuDao;
	@Autowired
	private TwoLevelMenuDao twoLevelMenuDao;
	@Autowired
	private PermDao permDao;
	
	/**
	 * 
	 */
	@Override
	public List<OneLevelMenu> getOneLevelMenus() {
		return oneLevelMenuDao.findAll();
	}

	@Override
	public List<TwoLevelMenu> getTwoLevelMenus(String oneLevelId) {
		return twoLevelMenuDao.findByOneLevelId(oneLevelId);
	}

	@Override
	public List<Perm> getPermsByTwoLevelId(String twoLevelId) {
		//查询非登录接口
		return permDao.getPermsByTwoLevelId(twoLevelId,DictConstants.ISBLANK_PERM_NO,DictConstants.NO_VALIDATE_NO);
	}
	
}
